package com.booksystem.campuslibrarymanagementsystem.controller.admin;

import com.booksystem.campuslibrarymanagementsystem.commen.request.adminrequest.SendEmailMangement.Getemails;
import com.booksystem.campuslibrarymanagementsystem.commen.request.adminrequest.SendEmailMangement.Sendemails;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Verifys;
import com.booksystem.campuslibrarymanagementsystem.commen.result.adminresult.SendEmailManagementR.GetEmailR;
import com.booksystem.campuslibrarymanagementsystem.entity.AccountTable;
import com.booksystem.campuslibrarymanagementsystem.service.AccountTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.AuditUtil;
import com.booksystem.campuslibrarymanagementsystem.utils.EmailSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//发送邮箱
@RestController
@RequestMapping("/Admin")
@Api(tags = "发送邮箱")
public class SendEmailController {
    private final String idt = "admin";
    Verifys verifys = new Verifys();
    AccountTable accountTable = new AccountTable();
    @Autowired
    private AccountTableService accountTableService;

    /**
     * 1.发送邮箱。
     *
     * @return Result.success();or return Result.fail();
     * @RequestBody Sendemails（token，email，content）
     */
    @PostMapping("/SendEmail")
    @ApiOperation(value = "发送邮箱", notes = "给用户发送邮箱信息")
    public Result SendEmail(@RequestBody Sendemails sendemails) {
        verifys = AuditUtil.verifyUtil(sendemails.getToken(),idt,accountTableService);
        System.out.println(verifys);
        switch (verifys.getMsg()) {
            case "成功":
                String email = sendemails.getEmail();
                List<AccountTable> accountTable1 = accountTableService.queryByEmail(email);
                if (accountTable1.isEmpty()) {
                    return Result.fail("没有找到该邮箱");
                } else {
                    EmailSender.sendEmails(email, sendemails.getContent());
                    System.out.println("邮箱发送成功");
                    return Result.success("成功", 1);
                }
            default:
                return Result.fail(verifys.getMsg());
        }
    }

    /**
     * 2.获取邮箱。
     *
     * @return Result.success();or return Result.fail();
     * @RequestBody Sendemails（token，email，content）
     */
    @PostMapping("/GetEmail")
    @ApiOperation(value = "获取邮箱", notes = "获取所有邮箱信息")
    public Result<List<GetEmailR>> GetEmail(@RequestBody Getemails getemails) {
        verifys = AuditUtil.verifyUtil(getemails.getToken(),idt,accountTableService);
        switch (verifys.getMsg()) {
            case "成功":
                List<GetEmailR> list = accountTableService.GetEmails();
                if (!list.isEmpty()) {
                    System.out.println("邮箱列表发送成功");
                    return Result.success(list, list.size());
                } else {
                    return Result.fail("没有找到邮箱数据");
                }
            default:
                return Result.fail(verifys.getMsg());
        }
    }

}

